package com.javaspirit.android.diary.activity;

import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.javaspirit.android.diary.R;
import com.javaspirit.android.diary.dal.DiaryContentProvider;
import com.javaspirit.android.diary.dal.NoteTable;
import com.javaspirit.android.diary.domain.Note;

public class ViewNoteFragment extends Fragment{
	
	private static final String TAG = "ViewNoteFragment";
	
	// These are the Contacts rows that we will retrieve.
	static final String[] NOTE_SUMMARY_PROJECTION = new String[] { 
		NoteTable.COLUMN_ID,
		NoteTable.COLUMN_DATE, 
		NoteTable.COLUMN_MOOD, 
		NoteTable.COLUMN_WEATHER, 
		NoteTable.COLUMN_LAST_MOD_TS, 
		NoteTable.COLUMN_NOTE };
	
	static final int[] NOTE_SUMMARY_LAYOUT = new int[] { 
		R.id.vinf_tv_id,
		R.id.vinf_tv_date, 
		R.id.vinf_tv_mood, 
		R.id.vinf_tv_weather, 
		R.id.vinf_tv_timestamp, 
		R.id.vinf_tv_note };
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		
		super.onActivityCreated(savedInstanceState);

	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		return inflater.inflate(R.layout.viewnotefragment, container, false);
		
	}

	
	/**
	 * Get Note from database
	 * @author CONVHD
	 *
	 */
	private class GetNoteAsyncTask extends AsyncTask<String, Void, Note>{

		@Override
		protected Note doInBackground(String... params) {
			Cursor _cursor = getActivity().getContentResolver().query(DiaryContentProvider.CONTENT_URI_NOTE, NOTE_SUMMARY_PROJECTION, NoteTable.COLUMN_ID+"=?", params, "");
			Note note = new Note();
			if(_cursor!=null){
				_cursor.moveToFirst();
				note.setId(_cursor.getLong(_cursor.getColumnIndex(NoteTable.COLUMN_ID)));
				note.setNote(_cursor.getString(_cursor.getColumnIndex(NoteTable.COLUMN_NOTE)));
				note.setMood(_cursor.getInt(_cursor.getColumnIndex(NoteTable.COLUMN_MOOD)));
				note.setWeather(_cursor.getInt(_cursor.getColumnIndex(NoteTable.COLUMN_WEATHER)));
				note.setDiaryDate(_cursor.getLong(_cursor.getColumnIndex(NoteTable.COLUMN_DATE)));
				_cursor.close();
			}
			return note;
		}
		
	     protected void onPostExecute(Note note) {
	         ((TextView)getActivity().findViewById(R.id.vinf_tv_id)).setText(String.valueOf(note.getId()));
	         ((TextView)getActivity().findViewById(R.id.vinf_tv_note)).setText(note.getNote());
	         ((TextView)getActivity().findViewById(R.id.vinf_tv_date)).setText(String.valueOf(note.getDiaryDate()));
	         ((TextView)getActivity().findViewById(R.id.vinf_tv_mood)).setText(String.valueOf(note.getMood()));
	         ((TextView)getActivity().findViewById(R.id.vinf_tv_weather)).setText(String.valueOf(note.getWeather()));

	     }
	     
	}


	@Override
	public void onResume() {
		super.onResume();
		long ID = getActivity().getIntent().getExtras().getLong(NoteTable.COLUMN_ID);
		Log.i(TAG, "On Resume with ID:"+ID);
		new GetNoteAsyncTask().execute(String.valueOf(ID));
	}
	
	
	
	
	
}
